package com.example.myblog.controller;

import com.example.myblog.dto.SiteDTO;
import com.example.myblog.entity.Category;
import com.example.myblog.entity.Tag;
import com.example.myblog.exception.BaseException;
import com.example.myblog.result.ResponseBean;
import com.example.myblog.service.CategoryService;
import com.example.myblog.service.SiteService;
import com.example.myblog.service.TagService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.AccessType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yx
 */
@RestController
public class SiteController {
    SiteService siteService;
    @Autowired
    public void setSiteService(SiteService siteService) {
        this.siteService = siteService;
    }
    CategoryService categoryService;
    TagService tagService;
    @Autowired
    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }
    @Autowired
    public void setTagService(TagService tagService) {
        this.tagService = tagService;
    }

    /**
     * 返回前端初始化页面需要的信息
     * @param token
     * @return
     * @throws BaseException
     * @throws JsonProcessingException
     */
    @GetMapping("/site")
    public ResponseBean site(@RequestHeader(value = "Authorization",required = false) String token) throws BaseException, JsonProcessingException {
        SiteDTO site = siteService.site(token);
        return ResponseBean.success(site);
    }

    @GetMapping("/categoryAndTag")
    public ResponseBean getCategoryAndTag() {
        Map<String, Object> map = new HashMap<>(2);
        List<Category> categories = categoryService.getBaseMapper().selectList(null);
        List<Tag> tags = tagService.getBaseMapper().selectList(null);
        map.put("category",categories);
        map.put("tag",tags);
        return ResponseBean.success(map);
    }
}
